﻿//
//  Copyright (c) 2008, WebAula S/A 
//  All rights reserved.
//
//  Authors : 
//
//           * Ivan Paulovich (ivan@100loop.com)
//           Blog: http://www.100loop.com/          
//           Messenger: ivanpaulovich@hotmail.com 
//           Twitter: @ivanpaulovich
//

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcThemes.Models;
using MvcThemes.Controllers;
using Core;
using Core.Domain.Repositories;
using Core.Domain.Model;
using System.IO;
using Shell32;

namespace MvcThemes.Controllers
{
    public class PostController : Controller
    {
        public ActionResult Index()
        {
            IRepository<Post> repo = new PostRepository();
            return View(repo.GetAll());
        }

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(Post post)
        {
            IRepository<Post> repo = new PostRepository();
            repo.Save(post);
            return RedirectToAction("Index");
        }

        public ActionResult Edit(int id)
        {
            IRepository<Post> repo = new PostRepository();
            return View(repo.GetById(id));
        }

        [HttpPost]
        public ActionResult Edit(int id, Post post)
        {
            IRepository<Post> repo = new PostRepository();
            repo.Update(post);
            return RedirectToAction("Index");
        }

        public ActionResult View(int id)
        {
            IRepository<Post> repo = new PostRepository();
            return View(repo.GetById(id));
        }

        public ActionResult Delete(int id)
        {
            IRepository<Post> repo = new PostRepository();
            repo.Delete(repo.GetById(id));
            return RedirectToAction("Index");
        }

        public ActionResult Themes()
        {
            HttpCookie themeCookie = Request.Cookies["Theme"];

            string theme = themeCookie != null ? themeCookie.Value : "Default";

            DirectoryInfo dir = new DirectoryInfo(Server.MapPath("~/Themes"));
            List<string> themes = new List<string>();

            foreach (DirectoryInfo folder in dir.GetDirectories())
            {
                themes.Add(folder.Name);
            }

            ViewBag.Themes = new SelectList(themes, theme);

            return PartialView();
        }

        [HttpPost]
        public ActionResult Themes(string theme)
        {
            HttpCookie themeCookie = new HttpCookie("Theme", theme);
            HttpContext.Response.Cookies.Add(themeCookie);

            DirectoryInfo dir = new DirectoryInfo(Server.MapPath("~/Themes"));
            List<string> themes = new List<string>();

            foreach(DirectoryInfo folder in dir.GetDirectories())
            {
                themes.Add(folder.Name);
            }

            ViewBag.Themes = new SelectList(themes, theme);

            return PartialView();
        }

        [NonAction]
        private static string UnzipFile(string inputFileName, string destinationPath)
        {
            Shell shell = new Shell();

            Folder sourceFolder = shell.NameSpace(inputFileName);
            Folder destinationFolder = shell.NameSpace(destinationPath);
            string outputFileName = sourceFolder.Items().Item(0).Name;

            destinationFolder.CopyHere(sourceFolder.Items(), 16);

            return outputFileName;
        }

        [HttpPost]
        public ActionResult FileUpload()
        {
            HttpPostedFileBase viewsFile = Request.Files["Views"];
            string viewsFileName = Server.MapPath("~/Uploads/") + Path.GetFileName(viewsFile.FileName);

            viewsFile.SaveAs(viewsFileName);

            UnzipFile(viewsFileName, Server.MapPath("~/Themes/"));

            return RedirectToAction("Index");
        }
    }
}
